package com.atguigu.web;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "/manager/bookServlet")
public class BookServlet extends BaseServlet {

    private BookService bookService = new BookServiceImpl();

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取请求参数 pageNo和pageSize
        Integer pageNo = WebUtils.parseInt(req.getParameter("pageNo"),1);
        Integer pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        // 调用bookService.page(pageNo,pageSize) : Page;
        Page<Book> page = bookService.page(pageNo,pageSize);

        // 设置首页的url地址
        page.setUrl("manager/bookServlet?action=page");

        // 保存数据到reqeust域对象中
        req.setAttribute("page",page);
        // 请求转发到 web/pages/manager/book_manager.jsp
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1 获取请求参数(如果有)
        // 2 调用bookService.queryBooks()
        List<Book> books = bookService.queryBooks();
        // 3 保存数据到Reqeust域中
        request.setAttribute("books",books);
        // 4 请求转发/pages/manager/book_manager.jsp页面
        /**
         * 请求转发的地址必须以斜杠打头,它表示http://ip:port/工程路径/   映射 到代码的web目录.<br/>
         */
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1 获取请求的参数,封装为book对象
        Book book = WebUtils.copyParamToBean(new Book(),request.getParameterMap());
        // 2 调用bookService.addBook( book );
        bookService.addBook(book);
        // 3 重定向到图书列表管理页面 /book/manager/bookServlet?action=list
        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=page&pageNo="
                + Integer.MAX_VALUE);
//        request.getRequestDispatcher("/manager/bookServlet?action=list").forward(request,response);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1 获取请求的参数 id  编号
        Integer id = WebUtils.parseInt(request.getParameter("id"),0);
        //2 调用bookService.deleteBookById(id);
        bookService.deleteBookById(id);
        //3 重定向回图书列表管理页面
        /**
         * request.getContextPath() ==>> /book
         */
        response.sendRedirect(request.getContextPath()
                + "/manager/bookServlet?action=page&pageNo="
                + request.getParameter("pageNo"));
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1 获取请求的参数 封装为book对象
        Book book = WebUtils.copyParamToBean(new Book(),request.getParameterMap());
        //2 调用bookService.updateBook(book)修改图书
        bookService.updateBook(book);
        //3 重定向图书列表管理页面
        /**
         * request.getContextPath() ==>> /book
         */
        response.sendRedirect(request.getContextPath()
                + "/manager/bookServlet?action=page&pageNo="
                + request.getParameter("pageNo"));
    }

    protected void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1 获取请求的参数 id
        Integer id = WebUtils.parseInt(request.getParameter("id"),0);
        // 2 调用 bookService.queryBookById(id):Book;
        Book book = bookService.queryBookById(id);
        // 3 保存到reqeust域中
        request.setAttribute("book",book);
        // 4 请求转发到/pages/manager/book_edit.jsp页面
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request,response);
    }


}
